package cn.com.gridinfo.sooc.core.my.setting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 实名认证
 * Created by 刘晓晨 on 2015/12/15.
 */
public class ShiMingActivity extends BaseSoocActivity {

    @InjectView(R.id.student_button)// 选择（学生）
            RadioButton studentButton;
    @InjectView(R.id.teacher_button)//选择（老师）
            RadioButton teacherButton;

    @InjectView(R.id.btn_send)//提交按钮
            Button btnSend;
    @InjectView(R.id.radio_group)// radioGroup
            RadioGroup radioGroup;
    @InjectView(R.id.edit_trueName)//真实姓名
            ClearEditText editTrueName;
    @InjectView(R.id.edit_personalId)// 身份证号
            ClearEditText editPersonalId;
    @InjectView(R.id.edit_pwd)// 登录密码
            ClearEditText editPwd;
    private int identitytype = 3;//认证类型 3：学生 2 ：老师
    private String type;
    private String trueName, idCardNumber, pwd;//定义3个全局变量 真实姓名，身份证号，密码
    MyDao dao;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shiming_activity_layout);
        ButterKnife.inject(this);
        dao = new MyDao(this);
        SpannableString ss = new SpannableString("为了账号安全需确认登录密码");
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        editPwd.setHint(new SpannableString(ss));
        //设置不能点击时候的按钮状态
        btnSend.setEnabled(false);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "实名认证";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();

    }

    // 监听输入框
    @OnTextChanged(value = {R.id.edit_trueName, R.id.edit_personalId, R.id.edit_pwd}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void OnTextChanged() {
        if (StringUtils.isNull(editTrueName.getText().toString().trim()) ||
                StringUtils.isNull(editPersonalId.getText().toString().trim()) ||
                StringUtils.isNull(editPwd.getText().toString().trim())) {
            btnSend.setEnabled(false);
        } else {
            btnSend.setEnabled(true);
        }
    }

    @OnClick(R.id.btn_send)
    void send() {
        if (studentButton.isChecked()) {
            identitytype = 3;//选中学生
            type = "学生";
        } else if (teacherButton.isChecked()) {
            identitytype = 2;//选中老师
            type = "老师";

        }
        trueName = editTrueName.getText().toString().trim();//拿到真实姓名
        idCardNumber = editPersonalId.getText().toString().trim();//拿到身份证号
        pwd = editPwd.getText().toString().trim();//拿到密码
        init();//请求接口
    }

    // 请求接口
    private void init() {
        dao.shiMing(trueName, idCardNumber, identitytype, pwd);
    }

    //请求成功返回数据
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        if (dao.status.equals("0")) {
            Intent intent = new Intent(this, RenzhengTypeActivity.class);
            intent.putExtra("xm", trueName = editTrueName.getText().toString().trim());//将姓名传出
            intent.putExtra("identitytype", identitytype+"");//将认证类型传出
            intent.putExtra("rztype", type);//将认证类型传出
            intent.putExtra("id", idCardNumber = editPersonalId.getText().toString().trim());//将身份证号传出
            this.setResult(RESULT_OK, intent);
            startActivity(intent);
            this.finish();
            AnimUtil.intentSlidIn(this);
        } else {
            MessageUtils.showImageShortToast(this, dao.statusCode);//显示错误信息
        }
    }
}
